#ifndef SAPMSGINTERFACEI_H
#define SAPMSGINTERFACEI_H

#include <QObject>
#include<QNetworkReply>
#include<QNetworkRequest>
#include<QJsonObject>
#include<QJsonArray>
#include<QUrl>
#include<QJsonDocument>
#include<QMap>
#include <QMutex>
#include<QtXml>
#include<QtCore/QCoreApplication>
#include<QDomDocument>

#include "getinstancedefine.h"
#include "comm/config/cfgsetting.h"
#include "queryServer/sapmsginterface.h"
#include "sqlConnect/sqlconnectionpool.h"
#include "singleProject/single_project.h"
#include "comm/datebaseConnectPool/connectpoolfactory.h"



struct WebServiceItiemI
{
    QString userBadge="";       // 员工工号
    QString LabelNo="";
    QString actionType="";      // 操作类型 WS01 WS02 WS03 ……
    QString ikey="";            // 唯一键
    QString UrlPath="";         //路径
    int restcount=0;            //发送计数 最多三次

    QJsonObject queryJson;      // JSON 请求数据 WS02,WS05
    QDomDocument doc;           // xml 请求数据 WS04
    QJsonArray postArray;       //WS06
    QTimer *timeout = nullptr;  //超时定时器
    bool process = false;       //已处理过
    QNetworkReply *NetworkRepItiem; //网络回复
};

//
struct retMsgStructI{
    QString iKey="";
    QString result=""; //"result":"S",           //结果, S为成功, E为错误
    QString message="";//"message":""            //系统讯息
    QString errorType="0";//错误类型
};

//struct sapPathI{
//   int  Id;
//   QString Path;
//   QString Action;
//   QString funcation;
//   QString para1;
//   QString para2;
//   QString para3;

//   QString Type;        //路径类型 发料时根据类型 选择路径
//   QString PathName;
//};

struct WebServiceI{
    QString LabelNo;
    QString UserBadge;
    int pathId;
};

struct CurrentSendI{
    QString UrlPath="";
    QJsonObject postJson;
    QDomDocument postdoc;           // xml 请求数据

    QString Errorinfo="";
    QByteArray ReplyData;
};


class sapMsgInterfaceI : public QObject
{
    Q_OBJECT

public:
//    REGISTERGETINSTANCE(sapMsgInterface)

    //static sapMsgInterface* getInstance();
    explicit sapMsgInterfaceI(QObject *parent = nullptr);
                       //平板连接主控状态标志
    QTimer *MsgTimer;
    CurrentSendI CurrentSendII;
    CurrentSendI CurrentSendI_;
    QMap<int ,sapPath >sapPathMap;
    QMap<QString ,int>currentIkeyMap;//SAP 数据 错误计时  最多只发 3次

    QMap<QString,WebServiceI>WebServiceMap;//待 请求

    void setSapPathMap(QMap<int,sapPath>sapPathMap0);           //读取数据库的路径配置信息


    void setCurrentSend(CurrentSendI CurrentSend0);
    CurrentSendI getCurrentSend();   //缓存最新提交的路径以及数据信息

    void setCurrentSend_(CurrentSendI CurrentSend0);
    CurrentSendI getCurrentSend_();   //缓存最新提交的路径以及数据信息

    /**************************** 赖俊龙 ***************************/
    bool isOlien;                                   // 当前连接状态
    void setIsLien(bool newisOlien);                // 设置当前连接状态
    bool TabletDisConnet(); //平板向主控http请求失败，服务器在线状态判断

public slots:

    //读取服务器回复
    void ReadResponse(QNetworkReply *Reply);
    void sapMsgTimer();
    void ONReplyError(WebServiceItiemI WebServiceItiemI);//重复发送机制

public:
    //返回数据解析  actionType= "0" 失败
    void analysReplyJson(QString iKey,QJsonDocument JsonAnalyse,QString actionType);

public:

    //<<<<<<<<<<接口方式  Get Request <<<<<<<<<<<<
    void getLabelMsg(QString LabelNo, QString UserBadge,int pathId);

    //<<<<<<<<<< POST 接口返回 <<<<<<<<<<<<
    QMap<QString,ReplyMsg>ReplyMsgMap;
    void setMsgRecv_Msg(QString iKey, QJsonObject ReplyJson,QString actionType);//Msg 接口返回
    ReplyMsg GetMsgRecv_Msg(QString iKey);

    void setMsgRecv_Mes(QString iKey,QJsonObject ReplyJson);//MES 接口返回
    ReplyMsg GetMsgRecv_Mes(QString iKey);


    /******************************************************************/
    //Get SAP 数据
    void setWebService_Get(QString LabelNo,QString UrlPath,QString UserBadge,QString Type);

    //post 数据给 SAP
    void setWebService_Post(QString iKey,QString UrlPath,QJsonObject postJson,
                            QString UserBadge,QString Type);

    //post 数据给 SAP
    void setWebService_Post_WS06(QString iKey,QString UrlPath,QJsonArray postJson,
                            QString UserBadge,QString Type);


    //post 数据给 SAP
    void setWebService_SAOP_Post(QString iKey,QString UrlPath,QDomDocument doc,
                            QString UserBadge,QString Type);

    /************************** 任务函数  ***************************/
    //new SAP Task
    void newSAPExcelInfo_POst(QString iKey,QJsonObject SAPJson,QString UserBadge,int pathId);

    //query SAP Task
    void querySAPExcelInfo_POst(QString iKey,QJsonObject SAPJson,QString UserBadge,int pathId);

    //query SAP Library
    void queryStation_POst(QString iKey,QJsonObject SAPJson,QString UserBadge,int pathId);

signals:

    void ReplyWebService(QJsonObject RequestJson, QJsonObject ReplyJson,QString UserBadge,QString ActionType);
    void ReplyError(WebServiceItiemI WebServiceItiemI);



private:

    QNetworkAccessManager NetworkMgr;
    QList<WebServiceItiemI >WebServiceItiemMap;
    QList<WebServiceItiemI > WebServiceItiemList;

//  static QMutex mutex;
//  static sapMsgInterface* instance;





public:
   int currentsapPath=17;//SAP路径选择
   int WebConut=2;int allWebConut=0;

};

#endif // SAPMSGINTERFACEI_H
